ConfRepo = new Object();

ConfRepo.getFieldSet1 = function() {

	return Ext.create('Ext.form.FieldSet', {
		title : 'Basic',
		defaults : {
			labelWidth : 175,
			anchor : '100%'
		},
		items : [ {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			defaultType : 'checkboxfield',
			items : [ {
				boxLabel : "Fund",
				inputValue : 'confirmRepoFundCmb',
				id : 'confRepoFundCmb',
				flex : 1,
				padding : '0 10 0 10',
			}, {
				boxLabel : "Issuer",
				inputValue : 'confirmRepoIssuer',
				id : 'confRepoIssuer',
				flex : 1,
				padding : '0 10 0 10',
			} ]
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			defaultType : 'checkboxfield',
			items : [ {
				boxLabel : "Value Date",
				inputValue : 'confirmRepoValueDt',
				id : 'confRepoValueDt',
				flex : 1,
				padding : '0 10 0 10',
			}, {
				boxLabel : "Maturity Date",
				inputValue : 'confirmRepoMaturityDt',
				id : 'confRepoMaturityDt',
				flex : 1,
				padding : '0 10 0 10',
			} ]
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			defaultType : 'checkboxfield',
			items : [ {
				boxLabel : 'Delevary Method',
				inputValue : 'confirmRepoDelevaryMethod',
				id : 'confirmRepoDelevaryMethod',
				flex : 1,
				padding : '0 10 0 10',
			}, {
				boxLabel : 'Dealer',
				inputValue : 'confirmRepoDealer',
				id : 'confirmRepoDealer',
				flex : 1,
				padding : '0 10 0 10',
			} ]
		}, {
			xtype : 'checkboxfield',
			boxLabel : 'Settlement Method',
			inputValue : 'confirmRepoSettlementMethod',
			id : 'confirmRepoSettlementMethod',
			flex : 1,
			padding : '0 10 0 10',
		} ]
	});
};

ConfRepo.getFieldSet2 = function() {

	return Ext.create('Ext.form.FieldSet', {
		title : 'Parameters',
		defaults : {
			labelWidth : 175
		},
		items : [ {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			defaultType : 'checkboxfield',
			items : [ {
				boxLabel : "Principle Value",
				inputValue : 'confirmRepoPrincipleVal',
				id : 'confRepoPrincipleVal',
				flex : 1,
				padding : '0 10 0 10',
			}, {
				boxLabel : "Actual Rate",
				inputValue : 'confirmRepoActRate',
				id : 'confRepoActRate',
				flex : 1,
				padding : '0 10 0 10',
			}, ]
		}, {
			xtype : 'checkboxfield',
			boxLabel : "Day Basis",
			inputValue : 'confirmRepoDayBasis',
			id : 'confRepoDayBasis',
			flex : 1,
			padding : '0 10 0 10',
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			defaultType : 'checkboxfield',
			items : [ {
				boxLabel : "Withholding Tax",
				inputValue : 'confirmRepoWithholdingCB',
				id : 'confRepoWithholdingCB',
				flex : 1,
				padding : '0 10 0 10',
			}, {
				boxLabel : "Withholding Tax Rate",
				inputValue : 'confirmRepoWithholdRate',
				id : 'confRepoWithholdRate',
				flex : 1,
				padding : '0 10 0 10',
			} ]
		} ]
	});
};

ConfRepo.getCenterPanel = function() {
	var filedset1 = ConfRepo.getFieldSet1();
	var filedset2 = ConfRepo.getFieldSet2();

	var formPanel = Ext.create('Ext.form.FormPanel', {
		bodyPadding : '20',
		id : 'confRepoFormPanel',
		autoScroll : true,
		frame : true,
		defaults : {
			labelWidth : 175,
			anchor : '68%',
		},
		fieldDefaults : {
			labelAlign : 'left',
			labelWidth : 150,
		},
		title : 'Confirm REPO',
		items : [ filedset1, filedset2 ],
		buttonAlign : 'center',
		buttons : [ {
			text : 'Cancel',
			handler : function() {
				App.Core.removeScreen();
			}
		}, {
			text : 'Reset',
			handler : function() {
				this.up('form').getForm().reset();
			}
		}, {
			text : 'Save',
			itemId : 'saveButton',
			handler : function() {
				var form = this.up('form').getForm();
				var valueSet = '';
				var localFieldSet = '';

				if (Ext.ComponentQuery.query('#confRepoFundCmb')[0].getValue()) {
					valueSet = valueSet + Ext.ComponentQuery.query('#confRepoFundCmb')[0].getSubmitValue() + ';';
					localFieldSet = localFieldSet + 'confRepoFundCmb;';
				}

				if (Ext.ComponentQuery.query('#confRepoIssuer')[0].getValue()) {
					valueSet = valueSet + Ext.ComponentQuery.query('#confRepoIssuer')[0].getSubmitValue() + ';';
					localFieldSet = localFieldSet + 'confRepoIssuer;';
				}

				if (Ext.ComponentQuery.query('#confRepoValueDt')[0].getValue()) {
					valueSet = valueSet + Ext.ComponentQuery.query('#confRepoValueDt')[0].getSubmitValue() + ';';
					localFieldSet = localFieldSet + 'confRepoValueDt;';
				}

				if (Ext.ComponentQuery.query('#confRepoMaturityDt')[0].getValue()) {
					valueSet = valueSet + Ext.ComponentQuery.query('#confRepoMaturityDt')[0].getSubmitValue() + ';';
					localFieldSet = localFieldSet + 'confRepoMaturityDt;';
				}

				if (Ext.ComponentQuery.query('#confRepoPrincipleVal')[0].getValue()) {
					valueSet = valueSet + Ext.ComponentQuery.query('#confRepoPrincipleVal')[0].getSubmitValue() + ';';
					localFieldSet = localFieldSet + 'confRepoPrincipleVal;';
				}

				if (Ext.ComponentQuery.query('#confRepoActRate')[0].getValue()) {
					valueSet = valueSet + Ext.ComponentQuery.query('#confRepoActRate')[0].getSubmitValue() + ';';
					localFieldSet = localFieldSet + 'confRepoActRate;';
				}

				if (Ext.ComponentQuery.query('#confRepoDayBasis')[0].getValue()) {
					valueSet = valueSet + Ext.ComponentQuery.query('#confRepoDayBasis')[0].getSubmitValue() + ';';
					localFieldSet = localFieldSet + 'confRepoDayBasis;';
				}

				if (Ext.ComponentQuery.query('#confRepoWithholdingCB')[0].getValue()) {
					valueSet = valueSet + Ext.ComponentQuery.query('#confRepoWithholdingCB')[0].getSubmitValue() + ';';
					localFieldSet = localFieldSet + 'confRepoWithholdingCB;';
				}

				if (Ext.ComponentQuery.query('#confRepoWithholdRate')[0].getValue()) {
					valueSet = valueSet + Ext.ComponentQuery.query('#confRepoWithholdRate')[0].getSubmitValue() + ';';
					localFieldSet = localFieldSet + 'confRepoWithholdRate;';
				}
				if (Ext.ComponentQuery.query('#confirmRepoSettlementMethod')[0].getValue()) {
					valueSet = valueSet + Ext.ComponentQuery.query('#confirmRepoSettlementMethod')[0].getSubmitValue() + ';';
					localFieldSet = localFieldSet + 'confirmRepoSettlementMethod;';
				}
				if (Ext.ComponentQuery.query('#confirmRepoDealer')[0].getValue()) {
					valueSet = valueSet + Ext.ComponentQuery.query('#confirmRepoDealer')[0].getSubmitValue() + ';';
					localFieldSet = localFieldSet + 'confirmRepoDealer;';
				}
				if (Ext.ComponentQuery.query('#confirmRepoDelevaryMethod')[0].getValue()) {
					valueSet = valueSet + Ext.ComponentQuery.query('#confirmRepoDelevaryMethod')[0].getSubmitValue() + ';';
					localFieldSet = localFieldSet + 'confirmRepoDelevaryMethod;';
				}
				form.url = 'newConfField.action';

				formPanel.setLoading('Saving...');
				form.submit({

					params : {
						'screen' : 'RecordRepo',
						'values' : valueSet,
						'localFields' : localFieldSet,
						'user' : 'SaleConfirmer',

					},
					success : function(form, action) {
						formPanel.setLoading(false);
						Ext.Msg.alert('Success', 'Fields Saved!.');
					},
					failure : function(form, action) {
						Ext.Msg.alert('Failed!', 'There is some error returned from the server.');
						formPanel.setLoading(false);
					}
				});
			}
		} ],
	// listeners : {
	// 'afterrender' : {
	// fn : function(thisField) {
	// thisField.setLoading('Receiving configs...');
	// }
	// }
	// }
	});

	Ext.Ajax.request({
		url : 'confLocalFields.action',
		params : {
			screen : 'RecordRepo',
		},
		success : function(response) {
			var obj = Ext.decode(response.responseText);
			confirmFields = obj.values;

			var confFieldsArrray = confirmFields.split(";");
			var i;
			var confFieldLocal;
			var confFieldQuery;
			for (i = 0; i < confFieldsArrray.length - 1; i++) {
				confFieldQuery = '#' + confFieldsArrray[i];
				confFieldLocal = Ext.ComponentQuery.query(confFieldQuery)[0];
				confFieldLocal.setValue(true);
			}
			// formPanel.setLoading(false);
		},
		failure : function(form, action) {
			// formPanel.setLoading(false);
			Ext.Msg.alert('Error!', 'Failed to load Confirmation Fields!');
		}
	});

	return formPanel;
};